{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7b80af65-0328-4de2-b38d-ec07d765bb57",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-14T20:52:34.217543Z",
     "end_time": "2023-12-14T20:52:50.714422Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 设置最多显示10行\n",
    "pd.set_option('display.max_rows', 10)\n",
    "df = pd.read_excel('待清洗的扑克牌数据集.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db31592b-1857-489e-a006-ca074fa03440",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-14T20:52:50.720033Z",
     "end_time": "2023-12-14T20:52:50.735651Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n0   1  NaN  大王\n1   2  NaN  小王",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找[花色]缺失的行\n",
    "df[df['花色'].isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5e0142db-dfdf-4b50-bc6f-3e21776f76ff",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-14T20:52:50.735651Z",
     "end_time": "2023-12-14T20:52:50.783361Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色 牌面\n3   3  黑桃♠  A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找完全重复的行\n",
    "df[df.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "599f8f79-3609-4715-942e-15f02bd4d64c",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-14T20:52:50.751441Z",
     "end_time": "2023-12-14T20:52:50.817460Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色 牌面\n3   3  黑桃♠  A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找编号重复的行\n",
    "df[df['编号'].duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "779f55a2-ca2b-4600-becb-6f6926ec315b",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-14T20:52:50.767572Z",
     "end_time": "2023-12-14T20:52:50.883295Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array(['大王', '小王', 'A', '30', 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 2, 3],\n      dtype=object)"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找牌面的所有唯一值\n",
    "df['牌面'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3e27af59-9931-409d-bb33-00f669003ea9",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-14T20:52:50.783361Z",
     "end_time": "2023-12-14T20:52:50.967471Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n4   5  黑桃♠  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>黑桃♠</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找牌面包含30的异常值\n",
    "df[df['牌面'].isin(['30'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n0   1  NaN  大王\n1   2  NaN  小王",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找王牌，模糊匹配\n",
    "df[df['牌面'].str.contains('王', na=False)]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T20:55:13.342996Z",
     "end_time": "2023-12-14T20:55:13.374737Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n0   1  NaN  大王\n1   2  NaN  小王\n2   3  黑桃♠   A\n3   3  黑桃♠   A\n4   5  黑桃♠  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>黑桃♠</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找编号在1到5之间的行\n",
    "df[df['编号'].between(1, 5)]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T20:56:21.099293Z",
     "end_time": "2023-12-14T20:56:21.147100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n0   1  NaN  大王\n1   2  NaN  小王\n2   3  黑桃♠   A\n3   3  黑桃♠   A\n4   5  黑桃♠  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>黑桃♠</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找编号在1到5之间的行\n",
    "df[(df['编号'] >= 1) & (df['编号'] <= 5)]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T20:57:36.650155Z",
     "end_time": "2023-12-14T20:57:36.681616Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n0   1  NaN  大王\n1   2  NaN  小王\n2   3  黑桃♠   A\n3   3  黑桃♠   A\n4   5  黑桃♠  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>黑桃♠</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找编号在1到5之间的行\n",
    "df[~((df['编号'] < 1) | (df['编号'] > 5))]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T20:58:28.115818Z",
     "end_time": "2023-12-14T20:58:28.131748Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色  牌面\n0    1  NaN  大王\n1    2  NaN  小王\n2    3  黑桃♠   A\n4    5  黑桃♠  30\n5    6  黑桃♠   4\n..  ..  ...  ..\n49  50  方块♦   9\n50  51  方块♦  10\n51  52  方块♦   J\n52  53  方块♦   Q\n53  54  方块♦   K\n\n[53 rows x 3 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>黑桃♠</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>6</td>\n      <td>黑桃♠</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>49</th>\n      <td>50</td>\n      <td>方块♦</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>50</th>\n      <td>51</td>\n      <td>方块♦</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>51</th>\n      <td>52</td>\n      <td>方块♦</td>\n      <td>J</td>\n    </tr>\n    <tr>\n      <th>52</th>\n      <td>53</td>\n      <td>方块♦</td>\n      <td>Q</td>\n    </tr>\n    <tr>\n      <th>53</th>\n      <td>54</td>\n      <td>方块♦</td>\n      <td>K</td>\n    </tr>\n  </tbody>\n</table>\n<p>53 rows × 3 columns</p>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 排除完全重复的行，默认保留第一行\n",
    "df.drop_duplicates()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T20:59:53.757213Z",
     "end_time": "2023-12-14T20:59:53.773665Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "# 排除重复后直接替换原来的数据框\n",
    "df.drop_duplicates(inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:07:01.409812Z",
     "end_time": "2023-12-14T21:07:01.467663Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色  牌面\n0    1  NaN  大王\n2    3  黑桃♠   A\n15  16  红心♥   A\n28  29  梅花♣   A\n41  42  方块♦   A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>红心♥</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>梅花♣</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>方块♦</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按某一列排除重复，默认保留第一行\n",
    "df.drop_duplicates(['花色'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:09:40.185287Z",
     "end_time": "2023-12-14T21:09:40.201046Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色  牌面\n1    2  NaN  小王\n14  15  黑桃♠   K\n27  28  红心♥   K\n40  41  梅花♣   K\n53  54  方块♦   K",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>15</td>\n      <td>黑桃♠</td>\n      <td>K</td>\n    </tr>\n    <tr>\n      <th>27</th>\n      <td>28</td>\n      <td>红心♥</td>\n      <td>K</td>\n    </tr>\n    <tr>\n      <th>40</th>\n      <td>41</td>\n      <td>梅花♣</td>\n      <td>K</td>\n    </tr>\n    <tr>\n      <th>53</th>\n      <td>54</td>\n      <td>方块♦</td>\n      <td>K</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按某一列排除重复，保留最后一行\n",
    "df.drop_duplicates(['花色'], keep='last')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:10:36.140489Z",
     "end_time": "2023-12-14T21:10:36.172254Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色 牌面\n2    3  黑桃♠  A\n15  16  红心♥  A\n28  29  梅花♣  A\n41  42  方块♦  A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>红心♥</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>梅花♣</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>方块♦</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不重复的花色\n",
    "color = df.drop_duplicates(['花色'])\n",
    "# 删除包含缺失值的行\n",
    "color.dropna()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:12:39.789023Z",
     "end_time": "2023-12-14T21:12:39.804846Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色  牌面\n0    1  NaN  大王\n2    3  黑桃♠   A\n15  16  红心♥   A\n28  29  梅花♣   A\n41  42  方块♦   A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>红心♥</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>梅花♣</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>方块♦</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除全部为空的行\n",
    "color.dropna(how='all')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:13:23.908697Z",
     "end_time": "2023-12-14T21:13:23.940631Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号  牌面\n0    1  大王\n2    3   A\n15  16   A\n28  29   A\n41  42   A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除包含缺失值的列\n",
    "color.dropna(axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:14:23.126534Z",
     "end_time": "2023-12-14T21:14:23.160680Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号     花色  牌面\n0    1  Joker  大王\n2    3    黑桃♠   A\n15  16    红心♥   A\n28  29    梅花♣   A\n41  42    方块♦   A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>Joker</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>红心♥</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>梅花♣</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>方块♦</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 补全缺失值\n",
    "color.fillna('Joker')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:15:15.352607Z",
     "end_time": "2023-12-14T21:15:15.391705Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_9420\\2189289539.py:2: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.\n",
      "  color.fillna(method='bfill')\n"
     ]
    },
    {
     "data": {
      "text/plain": "    编号   花色  牌面\n0    1  黑桃♠  大王\n2    3  黑桃♠   A\n15  16  红心♥   A\n28  29  梅花♣   A\n41  42  方块♦   A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>黑桃♠</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>红心♥</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>梅花♣</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>方块♦</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用后面的值填充\n",
    "color.fillna(method='bfill')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:16:23.185873Z",
     "end_time": "2023-12-14T21:16:23.202108Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色  牌面\n0    1    0  大王\n2    3  黑桃♠   1\n15  16  红心♥   A\n28  29  梅花♣   A\n41  42  方块♦   A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>0</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>16</td>\n      <td>红心♥</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>梅花♣</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>方块♦</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 为了演示，先指定一个缺失值\n",
    "color.loc[2, '牌面'] = np.nan\n",
    "color.fillna({'花色': 0, '牌面': 1})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:19:41.222352Z",
     "end_time": "2023-12-14T21:19:41.254342Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "# 补全缺失值\n",
    "df = df.fillna('Joker')\n",
    "# 排除重复值\n",
    "df = df.drop_duplicates()\n",
    "# 修改异常值\n",
    "df.loc[4, '牌面'] = 3\n",
    "# 增加一张缺少的牌\n",
    "df = df._append({'编号': 4, '花色': '黑桃♠', '牌面': 2}, ignore_index=True)\n",
    "# 按编号排序\n",
    "df = df.sort_values('编号')\n",
    "# 重置索引\n",
    "df = df.reset_index()\n",
    "# 删除多余的列\n",
    "df = df.drop(['index'], axis=1)\n",
    "# 把清洗好的数据保存到Excel文件\n",
    "df.to_excel('完成清洗的扑克牌数据.xlsx', index=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-14T21:41:25.368573Z",
     "end_time": "2023-12-14T21:41:25.401780Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
